#! /bin/sh

UPUSER=`pgrep xinit | xargs ps -o uname= -p`
source /home/$UPUSER/.profile

TMCONFIGPATH_NRM=/home/$UPUSER/.config/transmission-daemon
TMCONFIGPATH_AUX=/home/$UPUSER/.config/transmission-daemon_no-manual

try_run()
{
	[[ $TORPART == $TORPART_NRM ]] && nohup transmission-daemon -g $TMCONFIGPATH_NRM && echo "Transmisson (normal) is starting..."
	[[ $TORPART == $TORPART_AUX ]] && nohup transmission-daemon -g $TMCONFIGPATH_AUX && echo "Transmisson (auxilliary) is starting..."

	transmission-remote -U
	transmission-remote -D

	return 0
}

cycle()
{
	[ -z "`ifconfig tun0`" ] && echo "[`date`] VPN isn't up" && return 0
	[ -n "`pgrep transmission`" ] && echo "[`date`] Transmission is already running, skipping..." && return 0
	try_run ""
}

cycle ""

# while :
# do
#	cycle ""
#	sleep 180s
# done

exit 0
